<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;
header("Content-Type:text/html; charset=utf-8");

class IndexController extends Controller {

    private $user_id;

    public function _initialize()
    {
        if(!check_login()){
            $this->error("登录异常,请先登录","/Home/Login/index");
        }
        $this->user_id  =   session('user_id');
        $cart_num   =   M("user_category")->where(array("user_id"=>$this->user_id))->count()+0;
        $this->assign("cart_num",$cart_num);
    }

    //系统首页
    public function index(){

        $keywords   =   I("get.keywords");
        $info   =   M("users")->where(array("id"=>$this->user_id))->find();
        if (!$info['name']) {
            $this->error("请先完善信息","/Home/Index/information");
        }

        if($keywords)   {
            $sql['_string']     =   "name like '%{$keywords}%'";
            $lists  =   M("course")->where(array("status"=>'2',$sql))->order("id asc")->select();
        } else {
            $lists  =   M("course")->where(array("status"=>'2'))->order("id asc")->select();
        }


        $this->assign("info",$info);
        $this->assign("lists",$lists);
        $this->assign("keywords",$keywords);
        $this->display();
    }

    //我的选课
    public function course_list(){

        $keywords   =   I("get.keywords");
        $info   =   M("users")->where(array("id"=>$this->user_id))->find();
        if (!$info['name']) {
            $this->error("请先完善信息","/Home/Index/information");
        }

        if($keywords)   {
            $sql['_string']     =   "name like '%{$keywords}%'";
            $lists  =   M("elective e")
                ->join("course_course as c ON c.id = e.course_id","left")
                ->where(array("users_id"=>$this->user_id,$sql))
                ->order("add_time asc")
                ->field("e.id,e.credit,e.comment,e.course_id,e.add_time,c.name couName,c.credit credit,c.type type")
                ->select();
        } else {
            $lists  =   M("elective e")
                ->join("course_course as c ON c.id = e.course_id","left")
                ->where(array("users_id"=>$this->user_id))
                ->order("add_time asc")
                ->field("e.id,e.credit,e.comment,e.course_id,e.add_time,c.name couName,c.credit couCredit,c.type type")
                ->select();
//            $lists  =   M("elective")->where(array("users_id"=>$this->user_id))->order("add_time desc")->select();
        }


        $this->assign("info",$info);
        $this->assign("lists",$lists);
        $this->assign("keywords",$keywords);
        $this->display();
    }
    //我的成绩
    public function score(){

        $keywords   =   I("get.keywords");
        $info   =   M("users")->where(array("id"=>$this->user_id))->find();
        if (!$info['name']) {
            $this->error("请先完善信息","/Home/Index/information");
        }

        if($keywords)   {
            $sql['_string']     =   "name like '%{$keywords}%'";
            $lists  =   M("elective e")
                ->join("course_course as c ON c.id = e.course_id","left")
                ->where(array("e.users_id"=>$this->user_id,'e.status'=>'1',$sql))
                ->order("add_time asc")
                ->field("e.id,e.credit,e.comment,e.course_id,e.add_time,c.name couName,c.credit couCredit,c.type type")
                ->select();
        } else {
            $lists  =   M("elective e")
                ->join("course_course as c ON c.id = e.course_id","left")
                ->where(array("e.users_id"=>$this->user_id,'e.status'=>'1'))
                ->order("add_time asc")
                ->field("e.id,e.credit,e.comment,e.course_id,e.add_time,c.name couName,c.credit couCredit,c.type type")
                ->select();
//            $lists  =   M("elective")->where(array("users_id"=>$this->user_id))->order("add_time desc")->select();
        }


        $this->assign("info",$info);
        $this->assign("lists",$lists);
        $this->assign("keywords",$keywords);
        $this->display();
    }

    //课程信息
    public function scoreInfo() {
        $id     =   I("get.id");
        $user_score   =   M("elective")->where(array("id"=>$id))->find();
        $info   =   M("course")->where(array("id"=>$user_score['course_id']))->find();

        $info['day']   =  task_course_day($info['day']);
        $info['time']   =  task_course_time($info['time']);

        $this->assign("info",$info);
        $this->assign("user_score",$user_score);
        $this->display();
    }

    //课程信息
    public function courseInfo() {
        $id     =   I("get.id");
        $user_course = M("elective")->where(array("course_id"=>$id,'users_id'=>$this->user_id))->find();
        $info   =   M("course")->where(array("id"=>$id))->find();

        $info['day']   =  task_course_day($info['day']);
        $info['time']   =  task_course_time($info['time']);

        $this->assign("info",$info);
        $this->assign("user_course",$user_course);
        $this->display();
    }

    //选课
    public function choice () {
        $data   =   I("post.");

        $data['users_id']   =   $this->user_id;
        $data['add_time']   =   getTime();
        $res = M('elective')->add($data);
        if($res !== false){
            $res = 1;
        } else {
            $res = 2;
        }
        echo $res;
    }

    //退课
    public function delData()
    {
        $id     =   I('get.id');
        $table  =   I('get.table');
        $info   =   M($table)->where("id in ($id)")->find();

        if ($info) {
            $res    =   M($table)->where("id in ($id)")->delete();
            if($res !== false){
                if($info['pic']) unlink('.'.$info['pic']);
                if($info['mini_pic']) unlink('.'.$info['mini_pic']);
            }
        } else {
            $res = 2;
        }
        echo $res;
    }

    //个人中心
    public function personal() {
        $info   =   M("users")->where(array("id"=>$this->user_id))->find();
       /* $info  =   M('users u')
            ->join("ab_department as d ON d.id = u.dep_id","left")
            ->join("ab_class as c ON c.id = u.class_id","left")
            ->where(array("u.id"=>$this->user_id))
            ->field("u.id,u.student_id,u.pic,u.sex,u.name,u.phone,u.age,u.phone,u.email,u.qq,u.constellation,u.autograph,u.address,u.work_address,d.name depName,c.name className")
            ->find();*/

        /*if (!$info['name']) {
            $this->error("请先完善信息","/Home/Index/information");
        }*/
        $this->assign("info",$info);
        $this->display();
    }

    //修改信息
    public function information() {
        if($_POST){

            $file   =   $_FILES['pic'];
            $dir='./Uploads/users/';
            $data   =   I("post.");
            if(!is_dir($dir)) {
                mkdir($dir, 0777, true);
            }
            if(!checkEmail($data['email']) && $data['email'])
                $this->error("邮箱格式错误");
            if($file['name']){
                $getPic =   upPic($file,$dir);
                if($getPic['tag']   ==  '1'){
                    $this->error($getPic['msg']);
                }else{
                    $data['pic']    =   $getPic;
                    $oPic           =   M("users")->where(array("id"=>$this->user_id))->getField("pic");
                }
            }
            $res    =   M("users")->where(array("id"=>$this->user_id))->save($data);
            if($res     !== false){
                if($oPic)   unlink('.'.$oPic);
                $this->redirect("/Home/Index/information");
            }else $this->error("编辑失败");
        }else{
            $info   =   M("users")->where(array("id"=>$this->user_id))->find();
            $department   =   M("department")->select();

            if($info['dep_id']){
                $classes = M("class")->where(array("dep_id"=>$info['dep_id']))->select();
                $this->assign("classes",$classes);
            }

            $this->assign("info",$info);
            $this->assign("department",$department);
            $this->display();
        }
    }

    public function depClass () {
        $dep_id       =   I('get.depId');
        $data   =   M("class")->where(array("dep_id"=>$dep_id))->select();
        echo json_encode($data);
    }


}